package com.ios.controller;

import com.ios.dto.MessageExecution;
import com.ios.entity.Message;
import com.ios.enums.MessageStateEnum;
import com.ios.service.impl.MessageServiceImpl;
import com.ios.util.HttpServletRequestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value = "/message",method = {RequestMethod.POST})
public class MessageController {
    @Autowired
    private MessageServiceImpl messageService;

    @RequestMapping(value = "/publish")
    @ResponseBody
    public Map<String,Object> Publish(HttpServletRequest request){
        Map<String, Object> modelMap = new HashMap<String, Object>();
        /*取参数*/
        Integer disablerId = HttpServletRequestUtil.getInt(request,"disabler_id");
        String context = HttpServletRequestUtil.getString(request,"context");
        if (!(disablerId==-1 && context.equals(null))){
            MessageExecution me = messageService.PublishMessage(disablerId,context);
            if (me.getState()== MessageStateEnum.REVIEW_ING.getState()){
                modelMap.put("success",true);
                modelMap.put("Message",me.getMessage());
            }else {
                modelMap.put("success",false);
                modelMap.put("errMsg",me.getStateInfo());
            }
        }else {
            modelMap.put("success",false);
            modelMap.put("errMsg","参数不完整");
        }
        return modelMap;
    }

    @RequestMapping(value = "/delete")
    @ResponseBody
    public Map<String,Object> Delete(HttpServletRequest request){
        Map<String, Object> modelMap = new HashMap<String, Object>();
        /*取参数*/
        Integer Id = HttpServletRequestUtil.getInt(request,"id");
        if (Id!=-1){
            MessageExecution me = messageService.DeleteMessage(Id);
            if (me.getState()==MessageStateEnum.SUCCESS_DELECTE.getState()){
                modelMap.put("success",true);
                modelMap.put("Msg",me.getStateInfo());
            }else {
                modelMap.put("success",false);
                modelMap.put("errMsg",me.getStateInfo());
            }
        }else {
            modelMap.put("success",false);
            modelMap.put("errMsg","参数不完整");
        }
        return modelMap;
    }

    @RequestMapping(value = "/showall")
    @ResponseBody
    public Map<String,Object> ShowAll(HttpServletRequest request){
        Map<String, Object> modelMap = new HashMap<String, Object>();
        try {
            MessageExecution me = messageService.ShowAllMessage();
            if (me.getState()==MessageStateEnum.SUCCESS_SHOW.getState()){
                modelMap.put("success",true);
                modelMap.put("messagelist",me.getMessages());
            }else {
                modelMap.put("success",false);
                modelMap.put("errMsg",me.getStateInfo());
            }
        }catch (Exception e){
            modelMap.put("success",false);
            modelMap.put("errMsg",e.getMessage());
        }
        return modelMap;
    }

    @RequestMapping(value = "/showattention")
    @ResponseBody
    public Map<String,Object> ShowByAttention(HttpServletRequest request){
        Map<String, Object> modelMap = new HashMap<String, Object>();
        /*获取参数*/
        Integer disablerId = HttpServletRequestUtil.getInt(request,"disabler_id");
        if (disablerId!=-1){
            MessageExecution me = messageService.ShowMessageByDisablerId(disablerId);
            if (me.getState()==MessageStateEnum.SUCCESS_SHOW.getState()){
                modelMap.put("success",true);
                modelMap.put("attentionMessagelist",me.getMessages());
            }else {
                modelMap.put("success",false);
                modelMap.put("errMsg",me.getStateInfo());
            }
        }else {
            modelMap.put("success",false);
            modelMap.put("errMag","请输入完整参数");
        }
        return modelMap;
    }
}
